<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------


/**
 * 配送方式操作功能
 * @package  quanliadmin/app/system
 */

class pa_sendType {

	//入口
	public function execute() {

		if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1:

                    $this->sendTypeList();
                    break;

                case 2:
                    $this->sendAreaList();
                    break;

                default:
                    backendError();
                    break;

            }

        } else {

            backendError();

        }

	}

    private function sendAreaList() {

        global $G_PGLOB;

        quanli_include('module/base/system/mapAreaZoning.class.php');

        if (!isset($_REQUEST['sendTypeID']) || !is_numeric($_REQUEST['sendTypeID'])) {
            backendError();
        }

        //实例化配送区域类
        $mapAreaZoning = new mapAreaZoning($G_PGLOB->DBCA);

        $arg_array = array(
            'type'=>2
        );

        $area_array = $mapAreaZoning->searchMapArea( $arg_array );

        $G_PGLOB->smarty->assign('area_data', $area_array);

        $G_PGLOB->smarty->assign('sendTypeID', $_REQUEST['sendTypeID']);

        $G_PGLOB->smarty->display('system/sendAreaList.tpl');

    }

	
	//初始化页面
	private function sendTypeList () {

		global $G_PGLOB;

        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(12));

		$G_PGLOB->smarty->display('system/sendTypeList.tpl');

	}
	
	//打开新增、编辑页面
	private function showEdit () {

		global $G_PGLOB;

        $pay_type_array = array();

        try {

            //查询支付方式类型
            $item_array = $G_PGLOB->DBCA->stmt_query('SELECT type FROM sys_paytype GROUP BY type');
            if ($item_array) {
                foreach($item_array as &$item) {
                    if ($item->type == 4 || $item->type == 5)
                        continue;
                    else if ($item->type == 1)
                        $item->name = '货到付款';
                    else if ($item->type == 2)
                        $item->name = '在线支付';
                    else if ($item->type == 3)
                        $item->name = '汇款';
                    $pay_type_array[$item->type] = array(
                        'type' => $item->type,
                        'name' => $item->name,
                        'flg' => 0
                    );
                }

            }

            if (isset($_REQUEST['sendTypeID']) && !empty($_REQUEST['sendTypeID'])) {

                //实例化系统类
                $systemHandler = new systemHandler($G_PGLOB->DBCA);

                //查询配送方式
                $send_type_array = $systemHandler->searchSendType( $_REQUEST );
                if (!$send_type_array)
                    throw new Exception('配送方式信息没有找到', 404);

                //查询配送方式对应的支付方式
                $send_pay_array = $systemHandler->searchPayTypeFromSendPay( $_REQUEST );
                if ($send_pay_array) {

                    foreach($send_pay_array as &$send_pay) {
                        $pay_type = $send_pay['pay_type'];
                        if (isset($pay_type_array[$pay_type]))
                            $pay_type_array[$pay_type]['flg'] = 1;
                    }

                }

                $G_PGLOB->smarty->assign_by_ref('send_type_array', $send_type_array[0]);
                $G_PGLOB->smarty->assign('operFlg', 0);

            } else {
                $G_PGLOB->smarty->assign('operFlg', 1);
            }

            $G_PGLOB->smarty->assign_by_ref('pay_type_array', $pay_type_array);

            $G_PGLOB->smarty->display('system/sendTypeDetail.tpl');

        } catch(Exception $e) {

            if ($e->getCode() > 0)
                $G_PGLOB->display_error_page($e->getMessage());
            else {
                error_log($e->getMessage());
                $G_PGLOB->display_error_page('系统错误，请与系统管理员联系');
            }

        }

	}

}